home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / khgraf.doc < prev    next >
Encoding:
Text File  |  1994-05-30  |  12.2 KB  |  455 lines

  1. KNOW-HOW.GRAF
  2. Screen diagrams output.
  3. (C) Stepan S. Vartanov, 1993
  4.  
  5. AXE.H and AXE.CPP.
  6.      Abstract axe, horizontal or vertical:
  7.  
  8. #ifndef __AXE_H_
  9. #define __AXE_H_
  10.  
  11. #include "graphpp.h"
  12. #include <string.h>
  13.  
  14. struct Axe
  15.     {
  16.         int len_scr;    // Pixels, axe len
  17.         int* ticks;     // Ticks
  18.         int* sub_ticks; // Sub-ticks
  19.         char** labels;  // Ticks labels
  20.         int ticks_no;   // Number of ticks
  21.         int sub_ticks_no; 
  22.     Axe(int l, double start = 0, double end = 0,
  23.             int tick_no = 5, int* t = NULL,
  24.          int s_tick_no = 0, int* s = NULL,
  25.          char** lab = NULL);
  26.     ~Axe();
  27.  
  28.     virtual loc get_label_pos(loc, int )
  29.             { return loc(0, 0); }
  30. Draw axe
  31.     virtual void show_axe(loc, int) {}
  32. Draw axe and labels
  33.     void draw_axe(loc left_topt, int, int);
  34.     virtual void show_labels(loc) {}
  35. Auto calc labels positions
  36.     int calc_labels(double start, double end,
  37.        int number_of_ticks = 5, int number_of_sub_ticks = 0);
  38.     };
  39.  
  40. #endif __AXE_H_
  41.  
  42. H_V_AXES.H.
  43.      Horizontal or vertical axe.
  44.  
  45. #ifndef __HORIZ_VERT_AXES_H_
  46. #define __HORIZ_VERT_AXES_H_
  47.  
  48. #include "axe.h"
  49.  
  50. enum axes_dir  { HORIZ_AXE, VERT_AXE };
  51. Normal axe have labels on the left-down side
  52. enum axes_type { NORMAL_AXE, REVERSE_AXE };
  53.  
  54. class HV_Axes : public Axe
  55.     {
  56.     protected:
  57.         int type;  
  58.         int dir;   
  59.     public:
  60.         HV_Axes(int l, double start = 0, double end = 0,
  61.             int tick_no = 0, int* t = NULL,
  62.          int s_tick_no = 0, int* s = NULL,
  63.          char** lab = NULL);
  64.  
  65.         virtual void show_labels(loc label_position);
  66.         void set_type(int label_type, int label_dir);
  67.         virtual loc get_label_pos(loc, int );
  68.  
  69.         virtual void show_axes(loc left_top, int);
  70.     };
  71.  
  72. #endif __HORIZ_VERT_AXES_H_
  73.  
  74. Example:
  75.  
  76. void main()
  77.     {
  78.     int gdriver = DETECT, gmode;
  79.     initgraph(&gdriver, &gmode, "");
  80.  
  81.     HV_Axes a(300, -20, 80);
  82.     a.set_type(NORMAL_AXE, HORIZ_AXE);
  83.  
  84.     a.draw_axe(loc(40, 320), LIGHTBLUE, YELLOW);
  85.  
  86.     a.set_type(REVERSE_AXE, HORIZ_AXE);
  87.     a.draw_axe(loc(40, 20), LIGHTBLUE, YELLOW);
  88.  
  89.     a.set_type(NORMAL_AXE, VERT_AXE);
  90.     a.draw_axe(loc(40, 20), LIGHTBLUE, YELLOW);
  91.  
  92.     a.set_type(REVERSE_AXE, VERT_AXE);
  93.     a.draw_axe(loc(340, 20), LIGHTBLUE, YELLOW);
  94.  
  95.     HV_Axes a2(200, -25, 80);
  96.     a2.set_type(NORMAL_AXE, HORIZ_AXE);
  97.     a2.draw_axe(loc(420, 20), LIGHTBLUE, YELLOW);
  98.  
  99.     HV_Axes a3(200, -122, 83);
  100.     a3.set_type(NORMAL_AXE, HORIZ_AXE, VERT_DIR);
  101.     a3.draw_axe(loc(420, 60), LIGHTBLUE, YELLOW);
  102.  
  103.     HV_Axes a4(200, -5635, -3558);
  104.     a4.set_type(NORMAL_AXE, HORIZ_AXE, VERT_DIR);
  105.     a4.draw_axe(loc(420, 140), LIGHTBLUE, YELLOW);
  106.  
  107.     HV_Axes a5(200, 0, 1180);
  108.     a5.set_type(NORMAL_AXE, HORIZ_AXE, VERT_DIR);
  109.     a5.draw_axe(loc(420, 240), LIGHTBLUE, YELLOW);
  110.  
  111.   int t[] = { 0, 5000, 10000 };
  112.   int s[] = { 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000 };
  113.  
  114.     char* lab[] = { "First", "Second", "Third" };
  115.  
  116.     HV_Axes a1(200, 0, 0, 3, t, 9, s, lab);
  117.  
  118.     a1.set_type(NORMAL_AXE, HORIZ_AXE);
  119.     a1.draw_axe(loc(60, 300), LIGHTGREEN, LIGHTRED);
  120.  
  121.     a1.set_type(REVERSE_AXE, HORIZ_AXE);
  122.     a1.draw_axe(loc(60, 270), LIGHTGREEN, LIGHTRED);
  123.  
  124.     a1.set_type(NORMAL_AXE, VERT_AXE);
  125.     a1.draw_axe(loc(100, 40), LIGHTGREEN, LIGHTRED);
  126.  
  127.     a1.set_type(REVERSE_AXE, VERT_AXE);
  128.     a1.draw_axe(loc(120, 40), LIGHTGREEN, LIGHTRED);
  129.  
  130.     closegraph();
  131.     }
  132.  
  133.  
  134. File Axes2.
  135.      2 - 3 - 4 axes.
  136.  
  137. #ifndef __AXES2_H_
  138. #define  __AXES2_H_
  139.  
  140. #include "h_v_axes.h"
  141.  
  142. enum { HORIZ1, VERT1, HORIZ2, VERT2 };
  143.  
  144. class Axes2
  145.     {
  146.     protected:
  147.         HV_Axes* horiz_axe;
  148.         HV_Axes* vert_axe;
  149.         HV_Axes* horiz_axe_2;
  150.         HV_Axes* vert_axe_2;
  151.     public:
  152.         Axes2();
  153.         ~Axes2()
  154.          { delete horiz_axe; delete vert_axe;
  155.            delete horiz_axe_2; delete vert_axe_2; }
  156. Cross on 0, 0
  157.         void cross(rect coord, loc zero);
  158.         void set_axe(int which, int l, double start = 0,
  159.             double end = 0,
  160.             int tick_no = 0, int* t = NULL,
  161.          int s_tick_no = 0, int* s = NULL,
  162.          char** lab = NULL, int text_direction);
  163.  
  164.         void show(loc lt, int ax_col, int lab_col);
  165.     };
  166.  
  167. #endif __AXES2_H_
  168.  
  169. Example:
  170.  
  171. void main()
  172.     {
  173.     int gdriver = DETECT, gmode;
  174.     initgraph(&gdriver, &gmode, "");
  175.  
  176.     Axes2* a = new Axes2();
  177.  
  178.     a->set_axe(HORIZ1, 300, -20, 80);
  179.     a->set_axe(HORIZ2, 300, -20, 80);
  180.     a->set_axe(VERT1, 300, -20, 80);
  181.     a->set_axe(VERT2, 300, -20, 80);
  182.  
  183.     a->show(loc(40, 20), LIGHTBLUE, YELLOW);
  184.  
  185.     delete a;
  186.  
  187.     closegraph();
  188.     }
  189.  
  190.  
  191. File GRAF
  192.      ò-ô curves.
  193.  
  194. #ifndef __GRAF_H_
  195. #define __GRAF_H_
  196.  
  197. #include "marker.h"
  198.  
  199. enum { NO_GRAF, LINE_GRAF, MARKER_GRAF, COMBINED_GRAF,
  200.     BAR_GRAF, BAR_3D_GRAF, STACKED_BAR_GRAF };
  201.  
  202. struct Graf
  203.     {
  204.         int marker_type;
  205.         int line_type;
  206.         int attr;
  207.         int bak;
  208.         int size;   // Marker size
  209.         int fill;   // Fill (for bars)
  210.  
  211.         Graf(int m, int l, int a, int b, int s, int f)
  212.             { marker_type = m; line_type = l;  attr = a;
  213.               bak = b; size = s; fill = f; }
  214.         void set_type(int m, int l, int a, int b, int s,
  215.               int f)
  216.            { marker_type = m; line_type = l; attr = a;
  217.              bak = b; size = s; fill = f; }
  218.         void show(int* x, int* y, int numpoints, int x0,
  219.             int y0, int no = 0);
  220.         void set_type(int m_t, int l_t)
  221.          { line_type = l_t; marker_type = m_t; }
  222.         void set_param(int a, int b, int s, int f)
  223.          { attr = a; bak = b; size = s; fill = f; }
  224.     };
  225.  
  226. #endif __GRAF_H_
  227.  
  228. Example:
  229.  
  230. void main()
  231.     {
  232.     int gdriver = DETECT, gmode;
  233.     initgraph(&gdriver, &gmode, "");
  234.  
  235.     int x[] = { -50, -25, 0, 25, 50 };
  236.     int y[] = { -50, 30, -10, 20, 0 };
  237.  
  238.     Graf g(BAR, LINE_GRAF, LIGHTGREEN, RED, 4, SLASH_FILL);
  239.     g.show(x, y, 5, 60, 60);
  240.  
  241.     setviewport(120, 0, 240, 240, 1);
  242.     g.set_type(BAR, MARKER_GRAF, LIGHTGREEN, RED, 4, SLASH_FILL);
  243.     g.show(x, y, 5, 60, 60);
  244.  
  245.     setviewport(240, 0, 360, 240, 1);
  246.     g.set_type(BAR, COMBINED_GRAF, LIGHTGREEN, RED, 4, SLASH_FILL);
  247.     g.show(x, y, 5, 60, 60);
  248.  
  249.     setviewport(360, 0, 480, 240, 1);
  250.     g.set_type(BAR, BAR_GRAF, LIGHTGREEN, RED, 12, SLASH_FILL);
  251.     g.show(x, y, 5, 60, 60);
  252.  
  253.     setviewport(500, 0, 630, 240, 1);
  254.     g.set_type(BAR, BAR_3D_GRAF, LIGHTGREEN, RED, 12, SLASH_FILL);
  255.     g.show(x, y, 5, 60, 60);
  256.  
  257.     closegraph();
  258.     }
  259.  
  260.  
  261. File GRAFIC.
  262.      Shell for above functions. Also could save - load datasets
  263. to - from disk for memory economy.
  264.  
  265. #ifndef __GRAFIC_H_
  266. #define __GRAFIC_H_
  267.  
  268. #include "axes2.h"
  269. #include "graf.h"
  270.  
  271.  
  272. struct GrafData
  273.     {
  274.     Graf* graf;         
  275.     int* data_x;        // ò-array
  276.     int* data_y;        // ô-array
  277.     int num_points;     // Total points number
  278.     char* fileName;     // swap file
  279.  
  280. è«¡ßΓαπ¬Γ«α
  281.     GrafData(char attr_color, char back_color,
  282.         int marker_type, int line_type, int size_of_marker,
  283.         int fill_style, int* data_x, int* data_y,
  284.         int number_of_points, char* fName = "");
  285.     ~GrafData();
  286.     void load(); 
  287.     void save(); 
  288.     };
  289.  
  290. Draw axes and datasets:
  291.  
  292. class Grafic : public Axes2
  293.     {
  294.     protected:
  295.      int used;                    // Dataset number (1-15)
  296.      rect coord;                  // Clip
  297.      rect work_coord;             // Work area, excluding labels           
  298.      int ax_col; int lab_col;     // Colors
  299.      loc zero;
  300.      double zero_x;
  301.      double zero_y;
  302.      double len_x;
  303.      double len_y;
  304.      int grid_style;
  305.     public:
  306.      GrafData* arrays[15];
  307.      Grafic(rect clip_area, int axe_type, int lab,
  308.           int grafics_num = 5, int save = 0);
  309.      ~Grafic();
  310.  
  311.      rect get_work_coord() { return work_coord; }
  312.      loc get_colors() { return loc(ax_col, lab_col); }
  313.      void grid();
  314.      loc get_zero(double xmin, double ymin, double xmax,
  315.           double ymax);
  316.      int bar_width();
  317.      void get_x_array(int ar, int num, double* data);
  318.      void get_y_array(int ar, int num, double* data);
  319.      rect set_work_rect(rect r) { return work_coord = r; }
  320.      void set_rect(rect r) { coord = r; }
  321.      void redraw();
  322.      void show_axes();
  323.      int* get_stacked(int n);
  324.     };
  325.  
  326. #endif __GRAFIC_H_
  327.  
  328.  
  329. Example:
  330.  
  331. void main()
  332.     {
  333.     int gdriver = DETECT, gmode;
  334.     initgraph(&gdriver, &gmode, "");
  335.  
  336. static int t[] = { 0, 5000, 10000 };
  337. static int s[] = { 1111,  2222, 3333, 4444, 5555, 6666, 7777, 8888 };
  338.     static char* lab[] = { "First", "Second", "Third" };
  339.  
  340.     bar(0, 0, 639, 479);
  341.     rect rc = rect(5, 5, 639, 479);
  342.     rectangle(rc);
  343.     Grafic a(rc, BLUE, GREEN, 2, 1);
  344.  
  345.     rect r = a.set_work_rect(rect(90, 50, 450, 250));
  346.     // Auto calc ticks and sub_ticks
  347.     a.set_axe(HORIZ1, r.width() - 1, -1.00, 1.00, 5, NULL);
  348.     a.set_axe(HORIZ2, r.width() - 1, 0, 0, 3, t, 8, s, lab);
  349.     // Auto calc ticks and pass array of sub_ticks
  350.     a.set_axe(VERT1, r.height() - 1, -300, 600, 10, NULL, 8, s);
  351.  
  352.     a.set_axe(VERT2, r.height() - 1, 0, 0, 3, t, 8, s, lab);
  353.  
  354.     loc z = a.get_zero(-1.00, -300, 1.00, 600);
  355.  
  356.     a.grid();
  357.     setcolor(BLACK);
  358.     a.show_axes();
  359.  
  360.     z = z - a.get_work_coord().origin;
  361.     static double x_ar[] = { -.80, -.60, -.40, -.20, 0, .20, .40,
  362.                              .60, .80 };
  363.     static double y1_ar[] = { -200, -150, -100, -50, 0, 50, 100, 150,
  364.                               200 };
  365.     static double y2_ar[] = { -100, 100, -20, -5, 19, 100, 200, 100,
  366.                               100 };
  367.  
  368.     a.get_x_array(0, 9, x_ar);
  369.     a.get_y_array(0, 9, y1_ar);
  370.     a.get_x_array(1, 9, x_ar);
  371.     a.get_y_array(1, 9, y2_ar);
  372.     a.get_x_array(2, 9, x_ar);
  373.     a.get_y_array(2, 9, y2_ar);
  374.  
  375.     a.arrays[0]->graf->set_type(BAR, STACKED_BAR_GRAF);
  376.     a.arrays[1]->graf->set_type(BAR, STACKED_BAR_GRAF);
  377.     a.arrays[2]->graf->set_type(BAR, STACKED_BAR_GRAF);
  378.  
  379.     int w = a.bar_width();
  380.     a.arrays[0]->graf->set_param(RED, GREEN, w,
  381.      SLASH_FILL);
  382.     a.arrays[1]->graf->set_param(RED, CYAN, w,
  383.      LINE_FILL);
  384.     a.arrays[2]->graf->set_param(RED, RED, w,
  385.      XHATCH_FILL);
  386.  
  387.     setviewport(a.get_work_coord(), 1);
  388.  
  389.     int* array = a.get_stacked(0);
  390.     a.arrays[0]->graf->show(a.arrays[0]->data_x, array, 9,
  391.      z.X, z.Y);
  392.     delete array;
  393.     array = a.get_stacked(1);
  394.     a.arrays[1]->graf->show(a.arrays[1]->data_x, array, 9,
  395.      z.X,  z.Y);
  396.     delete array;
  397.     array = a.get_stacked(2);
  398.     a.arrays[2]->graf->show(a.arrays[2]->data_x, array, 9,
  399.      z.X,  z.Y);
  400.     delete array;
  401.  
  402.     setfillstyle(SOLID_FILL, WHITE);
  403.     setviewport(0, 0, getmaxx(), getmaxy(), 1);
  404.     bar(0, 0, 639, 479);
  405.  
  406.     Grafic a1(rc, BLUE, GREEN, 2, 1);
  407.  
  408.     r = a1.set_work_rect(rect(100, 100, 300, 300));
  409.  
  410.     a1.set_axe(HORIZ1, r.width() - 1, -1.00, 1.00, 5, NULL, 0, NULL,
  411.      NULL, VERT_DIR);
  412.     a1.set_axe(HORIZ2, r.width() - 1, 0, 0, 3, t, 8, s, lab);
  413.     a1.set_axe(VERT1, r.height() - 1, -300, 300, 10, NULL, 0, NULL);
  414.     a1.set_axe(VERT2, r.height() - 1, 0, 0, 3, t, 8, s, lab);
  415.  
  416.     z = a1.get_zero(-1.00, -300, 1.00, 300);
  417.  
  418.     a1.grid();
  419.     setcolor(BLACK);     // FOR CROSS
  420.     a1.show_axes();
  421.  
  422.     z = z - a1.get_work_coord().origin;
  423.  
  424.     a1.get_x_array(0, 9, x_ar);
  425.     a1.get_y_array(0, 9, y1_ar);
  426.     a1.get_x_array(1, 9, x_ar);
  427.     a1.get_y_array(1, 9, y2_ar);
  428.     a1.get_x_array(2, 9, x_ar);
  429.     a1.get_y_array(2, 9, y1_ar);
  430.  
  431.     a1.arrays[0]->graf->set_type(BAR, COMBINED_GRAF);
  432.     a1.arrays[1]->graf->set_type(CIRCLE, BAR_3D_GRAF);
  433.     a1.arrays[2]->graf->set_type(TRIANGLE, BAR_3D_GRAF);
  434.  
  435.     w = a1.bar_width();
  436.     a1.arrays[0]->graf->set_param(RED, GREEN, w,
  437.      SLASH_FILL);
  438.     a1.arrays[1]->graf->set_param(RED, CYAN, w,
  439.      LINE_FILL);
  440.     a1.arrays[2]->graf->set_param(RED, RED, w,
  441.      XHATCH_FILL);
  442.  
  443.     setviewport(a1.get_work_coord(), 1);
  444.  
  445.     a1.arrays[0]->graf->show(a1.arrays[0]->data_x,
  446.              a1.arrays[0]->data_y, 9, z.X, z.Y, 0);
  447.     a1.arrays[1]->graf->show(a1.arrays[1]->data_x,
  448.              a1.arrays[1]->data_y, 9, z.X,  z.Y, 1);
  449.     a1.arrays[2]->graf->show(a1.arrays[2]->data_x,
  450.              a1.arrays[2]->data_y, 9, z.X,  z.Y, 2);
  451.  
  452.     closegraph();
  453.     }
  454.  
  455.